import os
import re
os.system('chcp 65001')
def exec(cmd):
    result = os.popen(cmd)
    context = result.read()
    content = ""
    for line in context.splitlines():
        content += line
    return content

def get_ip_address():
    content = exec('adb shell ip addr show wlan0')
    match_obj = re.search(r'inet\s(\d+\.\d+\.\d+\.\d+).*?wlan0', content)
    if match_obj and match_obj.group(1):
        ip = match_obj.group(1)
        print("获取IP成功")
        return ip
    else:
        print('无法获取IP')
        return None

def connect(ip):
    content = exec("adb tcpip 5555")
    content = exec("adb kill-server")
    content = exec("adb connect {}".format(ip))
    print(content)

if __name__ == "__main__":
    ip = get_ip_address()
    if ip is None:
        exit(0)
    print(ip)
    connect(ip)





